package cn.zb.controller;

import cn.zb.api.user.vo.DeptVO;
import cn.zb.api.user.vo.RoleVO;
import cn.zb.api.user.vo.UserVO;
import cn.zb.common.util.Result;
import cn.zb.entity.SysDept;
import cn.zb.entity.SysRole;
import cn.zb.entity.SysUser;
import cn.zb.mapper.SysUserMapper;
import cn.zb.mapstruct.UserBeanMapper;
import cn.zb.service.SysDeptService;
import cn.zb.service.SysRoleService;
import cn.zb.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户控制器
 *
 * @author:
 * @date: 2024-11-23 15:47
 **/
@RestController
@RequestMapping("user")
@Api("用户控制器")
public class UserController {
    @Resource
    SysUserMapper userMapper;
    @Resource
    SysRoleService roleService;
    @Resource
    SysUserService userService;
    @Resource
    SysDeptService deptService;

    @PostMapping("roleMenu")
    @ApiOperation("根据用户名查询对应角色的菜单id")
    public Result<List<Integer>> roleMenu(String username){
        return Result.data(userMapper.roleMenu(username));
    }

    @PostMapping("roles")
    @ApiOperation("获取所有角色")
    public Result<List<RoleVO>> getAllRoles(){
        List<SysRole> roleList = roleService.list();
        List<RoleVO> roleVOS = UserBeanMapper.instance.ROLE_VO_LIST(roleList);
        return Result.data(roleVOS);
    }

    @PostMapping("users")
    @ApiOperation("按照部门id搜索用户")
    public Result<List<UserVO>> getAllUsers(String deptID){
        List<SysUser> users = userMapper.getUserByDept(deptID);
        List<UserVO> userVOS = UserBeanMapper.instance.USER_VO_LIST(users);
        userVOS.forEach(user->{
            SysDept dept = deptService.getById(user.getDeptId());
            user.setDeptName(dept.getDeptName());
        });
        return Result.data(userVOS);
    }

    @PostMapping("depts")
    @ApiOperation("获取所有部门")
    public Result<List<DeptVO>> getAllDepts(){
        List<SysDept> depts = deptService.list();
        List<DeptVO> deptVOS = UserBeanMapper.instance.DEPT_VO_LIST(depts);
        return Result.data(deptVOS);
    }
}
